package com.dfbz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfbz.entity.User;
import com.dfbz.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
@SpringBootTest(classes = MyBatisPlusApplication.class)
@RunWith(SpringRunner.class)
public class Demo01_BaseMapper的基本CURD {

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增
     * INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
     */
    @Test
    public void insert() {
        User user = new User(100L, "Ken", "0", 20);
        userMapper.insert(user);
    }

    /**
     * 修改
     * UPDATE user SET name=?, age=?, email=? WHERE id=?
     */
    @Test
    public void update() {
        User user = new User(100L, "Kevin", "0", 25);
        userMapper.updateById(user);
    }


    /**
     * 根据id查询
     * SELECT id,name,age,email FROM user WHERE id=?
     */
    @Test
    public void selectById() {
        User user = userMapper.selectById(100L);
        System.out.println(user);
    }

    /**
     * 根据一批id查询
     * SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
     */
    @Test
    public void selectBatchIds() {
        List<User> userList = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
        for (User user : userList) {
            System.out.println(user);
        }
    }

    /**
     * 根据条件查询数据
     * SELECT id,name,age,email FROM user WHERE name = ? AND id = ?
     */
    @Test
    public void selectByMap() {
        // 封装条件
        HashMap<String, Object> param = new HashMap<>();
        param.put("id", 10L);
        param.put("name", "Kevin");

        // 根据条件查询,map中的多个条件是并列关系 id=10 and name='小灰灰'
        List<User> userList = userMapper.selectByMap(param);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    /**
     * 根据id删除
     * DELETE FROM user WHERE id=?
     */
    @Test
    public void deleteById() {
        userMapper.deleteById(100L);
    }

    /**
     * 根据id删除一批
     * DELETE FROM user WHERE id IN ( ? , ? , ? )
     */
    @Test
    public void deleteBatchIds() {
        userMapper.deleteBatchIds(Arrays.asList(1, 2, 3));
    }

    /**
     * 根据条件删除数据
     * DELETE FROM user WHERE name = ? AND id = ?
     */
    @Test
    public void deleteByMap() {
        // 封装条件
        HashMap<String, Object> param = new HashMap<>();
        param.put("id", 100L);
        param.put("name", "Kevin");

        userMapper.deleteByMap(param);
    }

}

